# import copy

# class Solution:
#     def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
#         mapper={}
#         left=right=0
#         while right<len(A) and len(mapper.keys())!=K:
#             if A[right] in mapper:
#                 mapper[A[right]]+=1
#             else:
#                 mapper[A[right]]=1
#             right+=1
#         if right==len(A):
#             return 0
#         count=1
#         while right<len(A):
#             if A[right] in mapper:
#                 temp=left
#                 tmapper=copy.deepcopy(mapper)
#                 count+=1
#                 while len(tmapper.keys())==K:
#                     if tmapper[A[temp]]==1:
#                         del tmapper[A[temp]]
#                     else:
#                         tmapper[A[temp]]-=1
#                         count+=1
#                     temp+=1
#                 mapper[A[right]]+=1
#                 right+=1
#             else:
#                 mapper[A[right]]=1
#                 while len(mapper.keys())!=K:
#                     if mapper[A[left]]==1:
#                         del mapper[A[left]]
#                     else:
#                         mapper[A[left]]-=1
#                     left+=1
#                     count+=1
#                 right+=1
#         while len(mapper.keys())==K:
#             if mapper[A[left]]==1:
#                 del mapper[A[left]]
#             else:
#                 mapper[A[left]]-=1
#                 count+=1
#             left+=1
#         return count


import copy

class Solution:
    def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
        mapper1={}
        mapper2={}
        left1=left2=right=0
        count=0
        while right<len(A) and len(mapper1.keys())!=K:
            if A[right] in mapper1:
                mapper1[A[right]]+=1
                mapper2[A[right]]+=1
            else:
                mapper1[A[right]]=1
                mapper2[A[right]]=1
            right+=1
        if right==len(A):
            return 0
        while len(mapper2.keys())!=K-1:
            if mapper2[A[left2]]==1:
                del mapper2[A[left2]]
            else:
                mapper2[A[left2]]-=1
            left2+=1
        count+=left2-left1
        while right<len(A):
            if A[right] in mapper1:
                mapper1[A[right]]+=1
                if A[right] in mapper2:
                    mapper2[A[right]]+=1
                else:
                    mapper2[A[right]]=1
                while len(mapper2.keys())!=K-1:
                    if mapper2[A[left2]]==1:
                        del mapper2[A[left2]]
                    else:
                        mapper2[A[left2]]-=1
                    left2+=1
                count+=left2-left1
            else:
                mapper1[A[right]]=1
                mapper2[A[right]]=1
                while len(mapper1.keys())==K+1:
                    if mapper1[A[left1]]==1:
                        del mapper1[A[left1]]
                    else:
                        mapper1[A[left1]]-=1
                    left1+=1
                while len(mapper2.keys())==K:
                    if mapper2[A[left2]]==1:
                        del mapper2[A[left2]]
                    else:
                        mapper2[A[left2]]-=1
                    left2+=1                    
                count+=left2-left1
            right+=1
        return count


'''
执行用时：
484 ms
, 在所有 Python3 提交中击败了
89.22%
的用户
内存消耗：
17.5 MB
, 在所有 Python3 提交中击败了
37.72%
的用户
'''

s=Solution()
print(s.subarraysWithKDistinct([1,2,1,2,3],2))
